// 8_12.c
#include <stdio.h>
#define NUMEL 10

int binarySearch(int[], int, int); /*function prototype*/
int main()
{
    int nums[NUMEL] = {5, 10, 22, 32, 45, 67, 73, 98, 99, 101};
    int item, location;

    printf("Enter the item you are searching for: ");
    scanf("%d", &item);

    location = binarySearch(nums, NUMEL, item);

    if (location > -1)
        printf("The item was found at index location %d\n", location);
    else
        printf("The item was not found in the list\n");

    return 0;
}

#define TRUE 1
#define FALSE 0
/*this function returns the location of key in the list*/
/*a -1 is returned if the value is not found*/
int binarySearch(int list[], int size, int key)
{
    int index, found, left, right, midpt;
    index = -1;
    found = FALSE;
    left = 0;
    right = size - 1;
    while (left <= right && !found)
    {
        midpt = (int)((left + right) / 2);
        if (key == list[midpt])
        {
            found = TRUE;
            index = midpt;
        }
        else if (key > list[midpt])
            left = midpt + 1;
        else
            right = midpt - 1;
    }
    return (index);
}